/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controllers;

import Controllers.utils.HibernateUtil;
import Controllers.utils.URLUtils;
import Model.Client;
import java.io.IOException;
import java.util.HashMap;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.hibernate.Session;

/**
 *
 * @author Anthares
 */
public class Login extends BaseServlet {
    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request  servlet request
     * @param response servlet response
     * <p/>
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException      if an I/O error occurs
     */
    @Override
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        HashMap<String, String> requestedRessource = URLUtils.getRequestedRessource(getServletContext(), request);

        String action = requestedRessource.get("action");
        if(action == null){
            request.getRequestDispatcher("/login.jsp").forward(request, response);
        } else {
            if(action.equals("login")){
                loginAction(request, response);
            } else if(action.equals("logout")){
                logoutAction(request, response);
            } else {
                request.setAttribute("errorCode", 404);
                request.getRequestDispatcher("/error.jsp").forward(request, response);
            }
        }

    }

    private void loginAction(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession httpSession = request.getSession(true);

        String email = request.getParameter("email").trim();
        String password = request.getParameter("password").trim();

        Session session = HibernateUtil.getSessionFactory().openSession();
        Client user = (Client) session.getNamedQuery("Client.findByEMail").setString("eMail", email).uniqueResult();
        session.close();
        if(user == null || !user.getPassword().equals(password)){
            request.setAttribute("status", false);
            request.getRequestDispatcher("/login.jsp").forward(request, response);
        } else {
            httpSession.setAttribute("user", (Client) user);
            request.getRequestDispatcher("/home").forward(request, response);
        }
    }

    private void logoutAction(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession httpSess = request.getSession(false);
        httpSess.invalidate();
        request.getRequestDispatcher("/home").forward(request, response);
    }
}
